package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.utils.DataResult;
import com.company.project.common.utils.Func;
import com.company.project.entity.TMiddlenumEntity;
import com.company.project.entity.TSeatsEntity;
import com.company.project.mapper.TMiddlenumMapper;
import com.company.project.mapper.TSeatsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.TXiaohaoMapper;
import com.company.project.entity.TXiaohaoEntity;
import com.company.project.service.TXiaohaoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service("tXiaohaoService")
public class TXiaohaoServiceImpl extends ServiceImpl<TXiaohaoMapper, TXiaohaoEntity> implements TXiaohaoService {

    @Autowired
    public TXiaohaoMapper xiaohaoMapper;
    @Autowired
    private TSeatsMapper tSeatsMapper;
    @Autowired
    private TMiddlenumMapper tMiddlenumMapper;
    @Override
    public DataResult add(TXiaohaoEntity tXiaohao) {
        try {
            TXiaohaoEntity tXiaohaoEntity = new TXiaohaoEntity();
            tXiaohaoEntity.setTseatsid(tXiaohao.getTseatsid());
            //查询坐席
            TSeatsEntity tSeatsEntity = tSeatsMapper.selectById(tXiaohao.getTseatsid());
            tXiaohaoEntity.setAccountid(tSeatsEntity.getAccountid());

            //中间号id
            String zjhid = tXiaohao.getZjhid();
            TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectById(zjhid);

            //查询当前坐席已有小号
            TXiaohaoEntity tXiaohaoUser = xiaohaoMapper.selectOne(Wrappers.<TXiaohaoEntity>lambdaQuery()
                    .eq(TXiaohaoEntity::getTseatsid, tXiaohao.getTseatsid())
                    .eq(TXiaohaoEntity::getXiaohao,tMiddlenumEntity.getMiddlenum())

            );
            if (Func.isNotEmpty(tXiaohaoUser)) {
                return DataResult.fail("小号已存在，添加小号失败！");
            }

            Long integer = tMiddlenumMapper.selectCount(Wrappers.<TMiddlenumEntity>lambdaQuery()
                            .eq(TMiddlenumEntity::getId,zjhid)
                    .like(TMiddlenumEntity::getCompany, tSeatsEntity.getCorporatename()));
            if (integer == 0) {
                String company = tMiddlenumEntity.getCompany()==null?"":tMiddlenumEntity.getCompany();
                tMiddlenumEntity.setCompany(company+tSeatsEntity.getCorporatename()+";");
                //设置绑定公司
                tMiddlenumMapper.updateById(tMiddlenumEntity);
            }
            //剩余分钟数
            tXiaohaoEntity.setShengyucishu(tMiddlenumEntity.getSycs());
            tXiaohaoEntity.setXiaohao(tMiddlenumEntity.getMiddlenum());
            tXiaohaoEntity.setState("0");
            tXiaohaoEntity.setAddtime(new Date());
            tXiaohaoEntity.setZjhid(tMiddlenumEntity.getId());

            String id = tXiaohaoEntity.getId();
            boolean save = true;

             if(Func.isNotEmpty(id)) {
                save = updateById(tXiaohaoEntity);
            } else {
                save =save(tXiaohaoEntity);

            }
            return DataResult.success(save);
        } catch (Exception e) {
            e.printStackTrace();
            return DataResult.fail("添加小号异常，联系管理员！");
        }
    }
    @Override
    public void delete(List<String> ids) {
        //删除中间号绑定公司
        for (String id : ids) {
            TXiaohaoEntity tXiaohao = xiaohaoMapper.selectById(id);
            TSeatsEntity tSeatsEntity = tSeatsMapper.selectById(tXiaohao.getTseatsid());
            //公司名称
            String corporatename = tSeatsEntity.getCorporatename();
            TMiddlenumEntity tMiddlenumEntity = tMiddlenumMapper.selectById(tXiaohao.getZjhid());
            if (tMiddlenumEntity != null) {
                if (tMiddlenumEntity.getCompany().contains(corporatename)) {
                    tMiddlenumEntity.setCompany(tMiddlenumEntity.getCompany().replace(corporatename+";",""));
                    //设置绑定公司
                    tMiddlenumMapper.updateById(tMiddlenumEntity);
                }
            }

        }
    }

}